package com.epay.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.modular.system.entity.Parms;
import cn.stylefeng.guns.modular.system.entity.PlatMer;
import com.alibaba.fastjson.JSONObject;
import com.epay.Epay;
import com.epay.EpayEncrypt;
import com.epay.RespDataApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service("platMerService")
public class PlatMerService {


    @Autowired
    RedisTemplate redisTemplate;

    public PlatMer getByMerIdCache(Integer merId){


        return getByMerIdCache(merId,null);
    }
    public PlatMer getByMerIdCache(Integer merId,String merKey){
        Parms parms = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.AES_KEY_CODE);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("mId",merId);
        jsonObject.put("merKey",merKey);
        String iv = RandomUtil.randomNumbers(16);
        String content = EpayEncrypt.phpAesEncrypt(parms.getVal(),iv,jsonObject.toJSONString());

        Parms aes = (Parms) redisTemplate.opsForHash().get(Epay.PARAM_KEY, Epay.BaseParams.OPS_RECHARGE_API);
        String form = HttpUtil.createPost(aes.getVal() +"/platMer/getByMerIdCache.api"+ "?reqParam=" + content + "&iv=" + iv).execute().body();
        log.error(" update my getByMerIdCache response {} {} {}",form,jsonObject,aes.getVal() +"/platMer/getByMerIdCache.api"+ "?reqParam=" + content + "&iv=" + iv);

        PlatMer body  = JSONObject.parseObject(form ,PlatMer.class);





        return body;
    }
}
